const userId = localStorage.getItem("user_id");
//创建一个数组用来存储订单号
let tradeNoList = [];
//渲染订单表格
//当index = 0渲染已完成订单，当index = 1渲染未完成订单。
function renderOrderTab(index){
	ajax({
		method:"POST",
		type:"JSON",
		url:BASE_URL+"/api_order",
		params:"status=vieworder"+"&userId="+userId,
		callback:function(data){
			console.log(data);
			tradeNoList = [];
			let text = '';
			let orderTbody = document.querySelector("#orderTbody");
			if(index == 0){
				let ok = document.querySelector("#ok");
				ok.style.color = "#ededed";
				ok.style.background = "cornflowerblue";
				let no = document.querySelector("#no");
				no.style.color = "#666";
				no.style.background = "none";
				data.data.forEach((item,index)=>{
					tradeNoList.push(item.trade_no);
					if(item.status == "支付成功"){
						item.goods.forEach(good=>{
							let total = good.goods_number * good.price;
							text += "<tr>"+
								"<td height=50px class='left'>"+good.goods_name+"</td>"+
								"<td class='middle'>"+good.price+"</td>"+
								"<td class='middle'>"+good.goods_number+"</td>"+
								"<td class='middle'>"+item._id+"</td>"+
								"<td class='middle'>"+total+"</td>"+
								"<td class='middle'>支付成功</td>"+
								"<td class='right' onclick='delOrder("+index+",0)'>删除</td>"+
							"</tr>"
						})
					}
				})
				orderTbody.innerHTML = text;
			}
			if(index == 1){
				let no = document.querySelector("#no");
				no.style.color = "#ededed";
				no.style.background = "cornflowerblue";
				let ok = document.querySelector("#ok");
				ok.style.color = "#666";
				ok.style.background = "none";
				data.data.forEach((item,index)=>{
					tradeNoList.push(item.trade_no);
					if(item.status == "未支付"){
						item.goods.forEach(good=>{
							let total = good.goods_number * good.price;
							text += "<tr>"+
								"<td height=50px class='left'>"+good.goods_name+"</td>"+
								"<td class='middle'>"+good.price+"</td>"+
								"<td class='middle'>"+good.goods_number+"</td>"+
								"<td class='middle'>"+item._id+"</td>"+
								"<td class='middle'>"+total+"</td>"+
								"<td class='middle'>未支付</td>"+
								"<td class='right' onclick='delOrder("+index+",1)'>删除</td>"+
							"</tr>"
						})
					}
				})
				orderTbody.innerHTML = text;
			}
		}
	})
}

//删除订单
function delOrder(index,status){
	ajax({
		method:"POST",
		type:"JSON",
		url:BASE_URL+"/api_order",
		params:"status=delorder"+"&userId="+userId+"&tradeNo="+tradeNoList[index],
		callback:function(data){
			if(status == 0){
				renderOrderTab(0);
			}
			if(status == 1){
				renderOrderTab(1);
			}
		}
	})
}

window.onload = function(){
	let searchParams = new URLSearchParams(window.location.search);
	let tradeNo = searchParams.get('out_trade_no');
	if(tradeNo != null){
		new Promise((resolve,reject)=>{
			ajax({
				method:"POST",
				type:"JSON",
				url:BASE_URL+"/api_payquery",
				params:"userId="+userId+"&tradeNo="+tradeNo,
				callback:function(data){
					console.log(data);
					resolve();
				}
			})
		}).then((value)=>{
			renderOrderTab(0);
		})
	}
	else{renderOrderTab(0);}
	document.querySelector("#ok").addEventListener("click",()=>{
		renderOrderTab(0);
	})
	document.querySelector("#no").addEventListener("click",()=>{
		renderOrderTab(1);
	})
}